package com.orifine.orm.model;

import java.util.HashMap;
import java.util.Map;

import com.orifine.orm.annotation.Table;

public class DatabaseModel {
	private Map<Class<?>, TableModel> tableClassMap = new HashMap<Class<?>, TableModel>();
	
	public TableModel getTable(Class<?> tableClass){
		if(tableClass.getAnnotation(Table.class) != null){
			TableModel table = tableClassMap.get(tableClass);
			if(table != null){
				return table;
			} else {
				try {
					table = (TableModel) tableClass.getDeclaredField("table").get(null);
				} catch (SecurityException e) {
				} catch (NoSuchFieldException e) {
				} catch (IllegalArgumentException e) {
				} catch (IllegalAccessException e) {
				} catch (ClassCastException e){
				}
				
				if(table == null){
					table = new TableModel();
				}
				tableClassMap.put(tableClass, table);
				table.init(tableClass, this);
				return table;
			}
		}
		return null;
	}
	
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
}
